﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Tanmia.Core;
using Tanmia.Domain;

namespace Tanmia.Windows.DiscriminatedItemScenario
{
    /// <summary>
    /// Interaction logic for EasyAdd.xaml
    /// </summary>
    public partial class EasyAdd : Window
    {
        DiscriminatedItemDomain DIObj = new DiscriminatedItemDomain();

        List<DiscriminatedItem> DIList = new List<DiscriminatedItem>();
        DiscriminatedItem DI = new DiscriminatedItem();
        int position;

        bool isCancel = true;

        private IList<Tanmia.Core.Color> GetColors(Item item)
        {
            ColorDomain ColorObj = new ColorDomain();
            var reVal = ColorObj.GetsByCategory(item.Category);
            reVal.RemoveAt(0);
            return reVal;
        }

        private IList<Person> GetManufacturers()
        {
            PersonDomain PersonObj = new PersonDomain();
            var reVal = PersonObj.GetsByType(PersonTypeEnum.Manufacturer);
            reVal.RemoveAt(0);
            return reVal;
        }

        private void ShowDI(int position)
        {
            DI = DIList[position];
            txtColor.Text = DI.Color.Name;
            txtCost.Text = "";
            txtItem.Text = DI.Item.Name;
            txtManufacturer.Text = DI.Manufacturer.Name;
            txtPrice.Text = "";
            txtCost.Focus();
        }


        public EasyAdd(Item item, Unit defalutUnit)
        {
            InitializeComponent();

            UnitDomain UnitObj = new UnitDomain();

            var Units = UnitObj.Gets();

            cmbUnit.ItemsSource = Units;
            cmbUnit.DisplayMemberPath = "Name";


            foreach (var uni in Units)
            {
                if (uni.UnitID == defalutUnit.UnitID)
                {
                    cmbUnit.SelectedItem = uni;
                    break;
                }
            }

            var colors = GetColors(item);
            var manufacturers = GetManufacturers();

            var DItem = new DiscriminatedItemDomain();

            foreach (var col in colors)
            {
                foreach (var man in manufacturers)
                {
                    if (!DItem.IsDiscriminatedItemExisting(item, col, man))
                        DIList.Add(new DiscriminatedItem() { Color = col, CreateDate = DateTime.Now, Item = item, Manufacturer = man, Unit = cmbUnit.SelectedItem as Unit });
                }
            }

            position = 0;
            ShowDI(position);
        }

        public EasyAdd(Item item, Tanmia.Core.Color color, Unit defalutUnit)
        {
            InitializeComponent();

            UnitDomain UnitObj = new UnitDomain();

            var Units = UnitObj.Gets();

            cmbUnit.ItemsSource = Units;
            cmbUnit.DisplayMemberPath = "Name";


            foreach (var uni in Units)
            {
                if (uni.UnitID == defalutUnit.UnitID)
                {
                    cmbUnit.SelectedItem = uni;
                    break;
                }
            }

            var manufacturers = GetManufacturers();

            var DItem = new DiscriminatedItemDomain();

            foreach (var man in manufacturers)
            {
                if (!DItem.IsDiscriminatedItemExisting(item, color, man))
                    DIList.Add(new DiscriminatedItem() { Color = color, CreateDate = DateTime.Now, Item = item, Manufacturer = man, Unit = cmbUnit.SelectedItem as Unit });
            }

            position = 0;
            ShowDI(position);
            txtCost.Focus();
        }

        public EasyAdd(Item item, Person manufacturer, Unit defalutUnit)
        {
            InitializeComponent();

            UnitDomain UnitObj = new UnitDomain();

            var Units = UnitObj.Gets();

            cmbUnit.ItemsSource = Units;
            cmbUnit.DisplayMemberPath = "Name";

            foreach (var uni in Units)
            {
                if (uni.UnitID == defalutUnit.UnitID)
                {
                    cmbUnit.SelectedItem = uni;
                    break;
                }
            }

            var colors = GetColors(item);

            var DItem = new DiscriminatedItemDomain();

            foreach (var col in colors)
            {
                if (!DItem.IsDiscriminatedItemExisting(item, col, manufacturer))
                    DIList.Add(new DiscriminatedItem() { Color = col, CreateDate = DateTime.Now, Item = item, Manufacturer = manufacturer, Unit = cmbUnit.SelectedItem as Unit });
            }

            position = 0;
            ShowDI(position);
            txtCost.Focus();
        }


        private void btnIgnore_Click(object sender, RoutedEventArgs e)
        {
            if (position == DIList.Count - 1)
            {
                MessageBox.Show("تم الإنتهاء من الحفظ السريع!", "معلومة", MessageBoxButton.OK, MessageBoxImage.Information);
                isCancel = false;
                Close();
            }
            else
            {
                position++;
                ShowDI(position);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtCost.Text) || string.IsNullOrEmpty(txtPrice.Text))
                return;

            DI.Cost = double.Parse(txtCost.Text);
            DI.Price = double.Parse(txtPrice.Text);
            DI.Unit = cmbUnit.SelectedItem as Unit;

            DIObj.Save(DI);

            btnIgnore_Click(sender, e);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = isCancel;
        }
    }
}
